package data_report

import (
	"context"

	"gitee.com/liuxuezhan/ar-platform/rpc-client/analysisClient/rpc"
	"github.com/jinzhu/copier"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type MerchantReportLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 商户报表
func NewMerchantReportLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MerchantReportLogic {
	return &MerchantReportLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *MerchantReportLogic) MerchantReport(req *types.MerchantReportRequest) (resp *types.MerchantReportResponse, err error) {
	resp = &types.MerchantReportResponse{
		Data:     make([]types.MerchantReportData, 0),
		Page:     req.Page,
		PageSize: req.PageSize,
		Total:    0,
	}

	merchantAnalysisReq := &rpc.SearchArMerchantAnalysisReq{
		StartTime:    req.StartTime,
		EndTime:      req.EndTime,
		Type:         req.Type,
		MerchantName: req.MerchantName,
		Page:         req.Page,
		PageSize:     req.PageSize,
	}
	merchantAnalysisResp, err := l.svcCtx.Analysis.SearchArMerchantAnalysis(l.ctx, merchantAnalysisReq)
	if err != nil {
		return nil, err
	}

	copier.Copy(&resp.Data, merchantAnalysisResp.List)
	resp.Total = merchantAnalysisResp.Total
	return
}
